//
//  ModiViewController.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/3/24.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "ModiViewController.h"

@interface ModiViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oriPassword;
@property (weak, nonatomic) IBOutlet UITextField *newerPassword;
@property (weak, nonatomic) IBOutlet UITextField *rePassword;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation ModiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改密码";
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    _confirmBtn.layer.cornerRadius = 3;
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (IBAction)confirmAction:(id)sender {
    
    if ([_oriPassword.text isEqualToString:@" "] || _oriPassword.text.length == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入原来密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return;
    }
    
    if (_newerPassword.text.length == 0 || _rePassword.text.length == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入新的密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return;

    }
    
    
    if (_newerPassword.text.length < 6) {
        
        [Utils alertViewWithMessage:@"密码长度应为6位及以上"];
        return;
    }
    
    if (![_newerPassword.text isEqualToString:_rePassword.text]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码输入不一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return;

    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic setObject:@"api_user_changepass" forKey:@"Method"];
    [dic setObject:[Utils MD5EncryptWith:_oriPassword.text] forKey:@"oldpass"];
    [dic setObject:[Utils MD5EncryptWith:_newerPassword.text] forKey:@"newpass"];
    

    [dic setObject:USERID forKey:@"userid"];
    
    [Utils get:nil parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSLog(@"%@",jsonObject);
        NSDictionary *dict=(NSDictionary *)jsonObject;
        NSString *meg=[dict objectForKey:@"re_info"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:meg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    });

}
@end
